<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\HomeBaseController;
use think\Db;
class IndexController extends HomeBaseController
{
    public function index()
    {
        return $this->fetch(':index');
    }
    public function message_index(){
       // $data = $_POST;
        $data = [
            'name'=>$_POST['name'],
            'tel'=>$_POST['tel'],
            'ip'=>$_POST['ip'],
            'content'=>$_POST['content'],
            'time' => date("Y-m-d H:i:s",time()),
            'city'=>$_POST['city'],
        ];

        //用客户端ip 和 cookie 来验证是否多次提交留言表单
        $get_cookie_value = cookie('comment_token');
        $get_ip = md5(get_client_ip());
        if($get_cookie_value == $get_ip){
            $this->error('多次提交',null,"已经提交过了！");
            // $this->error('多次提交',null,cookie('comment_token')."====".get_client_ip());
        }else{
            cookie('comment_token',$get_ip,72000);
        }

        //验证下号码，没问题后提交数据库
        //$var = preg_match("/^1[34578]\d{9}$/", (string)$_POST['tel']);
        $var = $this->is_mobile($_POST['tel']);
        if($_POST['name'])
        if(!$var){
            $this->error('提交失败',null,"号码输入格式无法理解");
        }
        if($data['content'] ==""){
            $this->error('提交失败',null,"什么都没写啊！？");
        }else if(strlen($data['content']) >= 120){
            $this->error('提交失败',null,"请不要留言超过40个汉字！！");
        }else{
            Db::name('z_message_index')->insert($data);
            $this->success('提交成功',null,$var);
        }

    }
    public function contact(){
        return $this->fetch(':contact');
    }

    /**
     *   @Notes:验证号码:手机和座机
     *   @Function is_mobile
     * @return bool
     *   @author: 水沟小鱼
     *   @Time: 2018/11/7 0007   14:51
     * xxx
     */
   public function is_mobile($str=''){
       $isMob= preg_match("/^1[34578]{1}\d{9}$/", $str);

       $isTel= preg_match("/^([0-9]{3,4}-)?[0-9]{7,8}$/", $str);
       if($isMob || $isTel){
           return true;
       }else{
           return false;
       }
    }
}
